package main

/**
 * @Author：落叶知秋
 * @Date：15:08 2022/6/16
 * @note：
 **/
import (
	"flag"
	"load"
	"log"
	"os"
	"os/exec"
	"time"
)

func initialise() {
	//Command返回一个 *Cmd
	cbTerm := exec.Command("stty", "cbreak", "-echo")
	//stdin 是标准输入符的描述符
	//Stdin  = NewFile(uintptr(syscall.Stdin), "/dev/stdin")
	cbTerm.Stdin = os.Stdin

	err := cbTerm.Run()
	if err != nil {
		log.Fatalln("unable to activate cbreak mode:", err)
	}
}

//Cooked  mode 模式
func cleanup() {
	//Command返回一个 *Cmd
	cbTerm := exec.Command("stty", "cbreak", "-echo")
	//stdin 是标准输入符的描述符
	cbTerm.Stdin = os.Stdin

	err := cbTerm.Run()
	if err != nil {
		log.Fatalln("unable to cooked cbreak mode:", err)
	}
}

func proces(ch chan string) {
	for {
		intput, err := load.ReadInput()
		if err != nil {
			log.Println("error reading input:", err)
			ch <- "ESC"
		}
		ch <- intput
	}
}

var loadmap load.Loadmap

func initLoad() {
	loadmap.Lives = 3
	loadmap.Num_Pill = 0
	loadmap.Score = 0
}

var mazeFile = flag.String("maze_file", "maze01.txt", "path to custom maze file")

func main() {
	flag.Parse()
	// 初始化游戏
	initialise()
	initLoad()
	defer cleanup()
	// 加载地图
	err := loadmap.Loadmaze(*mazeFile) //找到了鬼和人的位置
	if err != nil {
		log.Println("failed to load maze:", err)
		return
	}
	//加载json
	//err = loadConfig(*configFile)
	//if err != nil {
	//	log.Println("failed to load configuration:",err)
	//	return
	//}
	// game loop
	//开启一个匿名协程，读操作
	input := make(chan string)
	go proces(input)
	//go func(ch chan <- string) {
	//	for{
	//		intput,err := readInput()
	//		if err != nil {
	//			log.Println("error reading input:",err)
	//			ch <- "ESC"
	//		}
	//		ch <- intput
	//	}
	//}(input)
	for {
		// process input

		// process movement

		select {
		case inp := <-input:
			if inp == "ESC" {
				loadmap.Lives = 0
			}
			loadmap.MovePlayer(inp)
		default:
		}
		loadmap.MoveGhost() //改变鬼的位置
		// process collisions   //判断是否碰撞
		loadmap.IsCollisions()

		//for _, g := range ghosts{
		//	if player.col == g.position.col && player.row == g.position.row{
		//
		//		if g.status == GhostStatusNormal {
		//			lives --
		//			if lives != 0{
		//				//位置光标移动到此处
		//				moveCursor(player.row,player.col)
		//				fmt.Print(cfg.Death) //死亡了，判定只要碰到就死了
		//				moveCursor(len(maze)+2 ,0)
		//				//更新，死了都重置了
		//				updateGhosts(ghosts,GhostStatusNormal)
		//				time.Sleep(200*time.Millisecond)
		//				//复原位置
		//				player.row, player.col = player.startRow, player.startCol
		//			}
		//		}else if g.status == GhostStatusBlue{
		//			updateGhosts([]*Ghost{g},GhostStatusNormal)
		//			g.position.row, g.position.col = g.position.startRow, g.position.startCol
		//		}
		//
		//	}
		//}

		//update screen
		loadmap.PrintScreen()
		// check game over
		if loadmap.IsLose() {
			break
		}
		if loadmap.IsWin() {
			break
		}
		//if score == 100 || num_Pill == 4 {
		//	moveCursor(player.row,player.col)
		//	fmt.Println(cfg.Win)
		//	moveCursor(player.startRow,player.startCol)
		//	fmt.Println("game victory!")
		//	moveCursor(len(maze)+2,0)
		//	break
		//}
		// Temp: break infinite loop

		// repeat
		time.Sleep(time.Millisecond * 200)
	}
}
